<?php
require("libraries/student.php");
class Checkrate extends Student{
	public function __construct(){
		parent::__construct();
		$this->load->database();
		$this->load->model("model_rate");
	}
	public function index(){
		$data['data'] = "";
		$data['act'] = 2;
		$data['listdata'] = array();
		$data['ticktype'] = 2;
		$data['title'] = "Manage periods of price";
		$data['listtrain'] = $this->model_rate->listtrains();
		$data["data"]["trainid"]=0;
		//$this->debug($data['listtrain']);
		if(isset($_POST["ok"])){
			$ticktype=isset($_POST["ticktype"])?$_POST["ticktype"]:1;
			$fromdate=strtotime($_POST["fromdate"]);
			$listdata=array();
			while($fromdate<=strtotime($_POST["todate"])){
				$listdata[date("Y-m-d",$fromdate)]=$this->model_rate->getalldata(date("Y-m-d",$fromdate),$_POST["trainid"],$ticktype);
				
				$fromdate+=3600*24;
			}
			
			$data["data"]["trainid"]=$_POST["trainid"];
			$data["data"]["listdata"]=$listdata;
			$data["data"]["ticktype"]=$_POST["ticktype"];
		}
		$data["template"]="checkrate/listall";
		$this->load->view("layout",$data);
	}
	public function updatefield(){
		if(!isset($_POST)){die();}
		$id = $_POST['id'];
		$filname = $_POST['field'];
		$val = $_POST['data'];
		$date = $_POST['date'];
		//echo $id." - ".$filname." - ".$val; die();
		$db = array(
			$filname => $val
		);
		$this->model_rate->updatefield($id,$db);
	}
	public function addtick(){	
		$data['data'] = "";
		$data['title'] = "Add or Update ticket";
		$data["template"]="checkrate/addtick";
		$data['act'] = 2;
		$data['listtrain'] = $this->model_rate->listtrains();
		if(isset($_POST["ok"])){
			if($_POST["trainid"] == 0){die("Please select a train");}
			$data["data"]["trainid"]=$_POST["trainid"];
			if(isset($_POST["oneway"])){
				for($i=0;$i<count($_POST["oneway"]["fromdate"]);$i++){
					$curdate=strtotime($_POST["oneway"]["fromdate"][$i]);
					while($curdate<=strtotime($_POST["oneway"]["todate"][$i])){
						$insertdata=array();
						$insertdata["tickdate"]=date("Y-m-d",$curdate);
						$insertdata["tickdeparts"]=$_POST["oneway"]["departs"][$i];
						$insertdata["tickarrives"]=$_POST["oneway"]["arrives"][$i];
						$insertdata["tickduration"]=$_POST["oneway"]["duration"][$i];
						$insertdata["softseat"]=$_POST["oneway"]["softseat"][$i];
						$insertdata["hardberth"]=$_POST["oneway"]["hardberth"][$i];
						$insertdata["softberth"]=$_POST["oneway"]["softberth"][$i];
						$insertdata["vipberth"]=$_POST["oneway"]["vipberth"][$i];
						$insertdata["ticknum"]=$_POST["oneway"]["ticknum"][$i];
						$insertdata["ticktype"]=1;
						$insertdata["trainid"]=$_POST["trainid"];
						$this->model_rate->updateall($insertdata["tickdate"],$insertdata["ticktype"],$insertdata["trainid"],$insertdata);
						//die();
						$curdate+=86400;
					}
				}
			}
			if(isset($_POST["roundtrip"])){
				for($i=0;$i<count($_POST["roundtrip"]["fromdate"]);$i++){
					$curdate=strtotime($_POST["roundtrip"]["fromdate"][$i]);
					while($curdate<=strtotime($_POST["roundtrip"]["todate"][$i])){
						$insertdata=array();
						$insertdata["tickdate"]=date("Y-m-d",$curdate);
						$insertdata["tickdeparts"]=$_POST["roundtrip"]["departs"][$i];
						$insertdata["tickarrives"]=$_POST["roundtrip"]["arrives"][$i];
						$insertdata["tickduration"]=$_POST["roundtrip"]["duration"][$i];
						$insertdata["softseat"]=$_POST["roundtrip"]["softseat"][$i];
						$insertdata["hardberth"]=$_POST["roundtrip"]["hardberth"][$i];
						$insertdata["softberth"]=$_POST["roundtrip"]["softberth"][$i];
						$insertdata["vipberth"]=$_POST["roundtrip"]["vipberth"][$i];
						$insertdata["ticknum"]=$_POST["roundtrip"]["ticknum"][$i];
						$insertdata["ticktype"]=2;
						$insertdata["trainid"]=$_POST["trainid"];
						$this->model_rate->updateall($insertdata["tickdate"],$insertdata["ticktype"],$insertdata["trainid"],$insertdata);
						$curdate+=86400;
					}
				}
			}
		}
		$this->load->view("layout",$data);
	}
	public function closeopen(){
		$data['act'] = 2;
		$data['title'] = "Open close ticket";
		$data["data"]=array();
		$data["template"]="checkrate/openclose";
		$data['listtrain'] = $this->model_rate->listtrains();
		$this->load->view("layout",$data);
	}
}